package cn.yuyiling.jelly.sr.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Properties;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(CorsConfig.class);

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        CorsConfigManager configManager = new CorsConfigManager();
        String addMapping = configManager.getAddMapping();
        String allowedOrigins = configManager.getAllowedOrigins();
        String allowedHeaders = configManager.getAllowedHeaders();
        String exposedHeaders = configManager.getExposedHeaders();
        int maxAge = configManager.getMaxAge();
        logger.info("CorsConfig init {}", addMapping);
        logger.info("CorsConfig init {}", allowedOrigins);
        logger.info("CorsConfig init {}", allowedHeaders);
        logger.info("CorsConfig init {}", exposedHeaders);
        logger.info("CorsConfig init {}", maxAge);


        registry.addMapping(addMapping) // 匹配所有接口路径
                .allowedOrigins(allowedOrigins) // 允许的前端地址
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 支持的方法
                .allowedHeaders(allowedHeaders) // 允许的请求头
                .exposedHeaders(exposedHeaders) // 可选：暴露给前端的响应头
                .allowCredentials(true) // 是否允许发送 Cookie
                .maxAge(maxAge); // 预检请求缓存时间（秒）
    }
}